package com.dimples.dd.system.controller;

import com.dimples.dd.common.constant.DDServerConstant;
import com.dimples.dd.common.result.CommonResult;
import com.dimples.dd.log.core.annotation.ApiLog;
import com.dimples.dd.log.core.enums.OperateTypeEnum;
import com.dimples.dd.system.model.form.RoleHomeForm;
import com.dimples.dd.system.service.PermissionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.simpleframework.xml.core.Validate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * 权限 Controller，提供赋予用户、角色的权限的 API 接口
 *
 * @author zhongyj <1126834403@qq.com><br/>
 * @date 2024/7/13
 */
@Validate
@Tag(name = "04.用户角色权限接口")
@RestController
@RequestMapping("permission")
@RequiredArgsConstructor
public class SysPermissionController {

    private final PermissionService permissionService;

    @Operation(summary = "获取角色的菜单ID集合")
    @GetMapping("/role/{roleId}/menus")
    @PreAuthorize("@ss.hasPerm('system:permission:assign-role-menu')")
    @ApiLog(operateType = OperateTypeEnum.SELECT, applicationName = DDServerConstant.DD_SERVER_SYSTEM)
    public CommonResult<List<Long>> getRoleMenuIds(
            @Parameter(description = "角色ID") @PathVariable Long roleId
    ) {
        List<Long> menuIds = this.permissionService.getRoleMenuIds(roleId);
        return CommonResult.success(menuIds);
    }

    @Operation(summary = "获取角色的首页")
    @GetMapping("/role/{roleId}/home-page")
    @PreAuthorize("@ss.hasPerm('system:permission:assign-role-menu')")
    @ApiLog(operateType = OperateTypeEnum.SELECT, applicationName = DDServerConstant.DD_SERVER_SYSTEM, responseEnable = true)
    public CommonResult<String> getRoleHomePage(
            @Parameter(description = "角色ID") @PathVariable Long roleId
    ) {
        String home = this.permissionService.getRoleHomePage(roleId);
        return CommonResult.success(home);
    }

    @Operation(summary = "赋予角色首页")
    @PutMapping("/role/{roleId}/home-page")
    @PreAuthorize("@ss.hasPerm('system:permission:assign-role-menu')")
    @ApiLog(operateType = OperateTypeEnum.UPDATE, applicationName = DDServerConstant.DD_SERVER_SYSTEM, responseEnable = true)
    public CommonResult<Boolean> updateRoleHomePage(
            @Parameter(description = "角色ID") @NotNull(message = "不能为空") @PathVariable Long roleId,
            @Parameter(description = "首页编码") @Valid @RequestBody RoleHomeForm roleHomeForm
    ) {
        roleHomeForm.setRoleId(roleId);
        this.permissionService.updateRoleHomePage(roleHomeForm);
        return CommonResult.success(true);
    }

    @Operation(summary = "赋予角色菜单")
    @PostMapping("/role/{roleId}/menus")
    @PreAuthorize("@ss.hasPerm('system:permission:assign-role-menu')")
    @ApiLog(operateType = OperateTypeEnum.UPDATE, applicationName = DDServerConstant.DD_SERVER_SYSTEM, responseEnable = true)
    public CommonResult<Boolean> assignRoleMenu(
            @PathVariable Long roleId,
            @RequestBody Set<Long> menuIds
    ) {
        this.permissionService.assignRoleMenu(roleId, menuIds);
        return CommonResult.success(true);
    }

    @Operation(summary = "赋予用户角色")
    @PostMapping("/user/{userId}/roles")
    @PreAuthorize("@ss.hasPerm('system:permission:assign-user-role')")
    @ApiLog(operateType = OperateTypeEnum.UPDATE, applicationName = DDServerConstant.DD_SERVER_SYSTEM, responseEnable = true)
    public CommonResult<Boolean> assignUserRole(
            @PathVariable Long userId,
            @RequestBody Set<String> roleCodes) {
        this.permissionService.assignUserRole(userId, roleCodes);
        return CommonResult.success(true);
    }
}



















